package edu.mse.camel.client.preference;

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import edu.mse.camel.client.serverlib.Activator;

public class CamelServerPage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {
	public  static final String IP = "Server IP";
	
	public CamelServerPage() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("Set Camel Server IP address or DNS name ");
		initializeDefaults();
	}
	@Override
	protected void createFieldEditors() {
		addField(new StringFieldEditor(IP, "Camel Server IP or DNS name", getFieldEditorParent()));
	}
	public void initializeDefaults() {	
		// we set the value in Activator now
		//IPreferenceStore store = Activator.getDefault().getPreferenceStore();
        //store.setDefault(IP, "buzzers.webhop.net");
   	}
	
	public void init(IWorkbench workbench) {	
	
   	}

	@Override
	public boolean performOk() {
		// TODO Auto-generated method stub
		
		if(changed) {
			MessageBox messageBox = new MessageBox(new Shell(), SWT.OK);
			messageBox.setText("Please restart eclipse");
			messageBox.setMessage("The camel server IP address has been changed. Please restart eclipse.");
			messageBox.open();
		}
		/*
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
		store.setValue(IP, newAddress);
		*/
		return super.performOk();
	}
	
	private String newAddress;
	private boolean changed = false;
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		// TODO Auto-generated method stub
		super.propertyChange(event);
		
		newAddress = (String)event.getNewValue();
		changed = true;
	}
	
}
